#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient


# 连接MongoDB
def InitMongoDB():
    MONGODB_HOST = '10.188.188.22'
    MONGODB_PORT = 27017
    MONGODB_DB_NAME = 'Dictionary'
    MONGODB_COLLECTION_NAME = 'HGNC_TEST_20170511'
    client = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = client[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return client, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()


def GenerateHGNCTest(filepath, MongoDBCollection):
    infile = open(filepath, 'r')
    headline = infile.readline().rstrip("\n")
    split_headline = headline.split("\t")
    for i in range(0, len(split_headline)):
        split_headline[i] = split_headline[i].replace(".", "_")
    for line in infile:
        insertcontent = {}
        line = line.rstrip("\n")
        splitline = line.split("\t")
        for i in range(0, len(splitline)):
            cell = splitline[i].strip('"')
            cell = cell.strip()
            if cell != "":
                content = []
                splitcell = cell.split("|")
                for item in splitcell:
                    content.append(item)
                insertcontent[split_headline[i]] = content
        MongoDBCollection.insert_one(insertcontent)


def ParseHGNC(filePath, configFilePath, MongoDBCollection):
    print("haha")


if __name__ == "__main__":
    MongoDBClient, MongoDBCollection = InitMongoDB()
    GenerateHGNCTest("/home/genesis/Data/HGNC/hgnc_complete_set.txt", MongoDBCollection)
    CloseMongoDB(MongoDBClient)
